﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using Autofac;
using AutoMapper;
using TpWeb.Dal.temp;
using TpWeb.Domain.BizObj;
using TpWeb.Domain.Repositories;
using TpWeb.Models;
using Country = TpWeb.Domain.BizObj.Country;
using System.Runtime.InteropServices;

namespace TpWeb.Controllers
{
    public class RegistrationController : Controller
    {
        private readonly ICountryRepository _countryRepository;
        private IContactRepository _contactRepository;
        private readonly IPersonRepository _personRepository;

        /*  public RegistrationController()
          {
              _countryRepository = MvcApplication.container.Resolve<ICountryRepository>();
          }*/

        public RegistrationController(ICountryRepository countryRepository, IContactRepository contactRepository,
            IPersonRepository personRepository)
        {
            _countryRepository = countryRepository;
            _contactRepository = contactRepository;
            _personRepository = personRepository;
        }

        //
        // GET: /Person/
        
        public ActionResult Index()
        {
            return View(new RegisterPersonViewModel()
            {
                FirstName = "Petr",
                LastName = "Vasil'ev",
                MiddleName = "Antonovich",
                BirthDate = new DateTime(2000, 01, 01),
                DocDate = new DateTime(2009, 01, 01),
                DocIssue = "UVD Upir'",
                Email = "petr@upir.ps",
                Nationality = "POLSHA",
                Phone = "123123",
                DocNum = "11 55 226339",
                NationalityCode = "PL",
                SecretAnswer = "4"

            }

                );
        }

        [HttpPost]
        public ActionResult Index(RegisterPersonViewModel model)
        {
            var person = model.ToPerson();
            if (ModelState.IsValid)
            {
                _personRepository.Insert(person);
                return RedirectToAction("Addresses", new { id = person.Id });
            }

            return View(model);
        }

        [HttpPost]
        public JsonResult CheckPin(int id, string pin)
        {
            if (_contactRepository.CheckPin(id, pin))
                return Json(new { error = false, text = "Код верный" });

            return Json(new { error = true, text = "Введен неверный код. НОвый код отправлен Вам на почту" });
        }

        [HttpPost]
        public ContentResult SaveContact(Contact model)
        {
            model.Created = DateTime.Now;
            model.Pin = Guid.NewGuid().ToString();
            _contactRepository.Insert(model);
            return Content(model.Id.ToString());
        }

        /*[HttpPost]
        public ActionResult GetCountriesJSON(string term, int maxCount)
        {
            var q = from c in _countryRepository.All
                    where c.Name.Contains(term)
                    orderby c.Name
                    select new CountryViewModel() { Code = c.Id, Name = c.Name };

            var res = q.Take(5).ToArray();

            return Json(res);
        }*/

        //[HttpPost]
        public ActionResult Addresses()
        {
            return View();
        }


    }
}